package com.allwees.bs.module.pms.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.allwees.bs.core.model.product.entity.CategoryModel;
import com.allwees.bs.core.modelbase.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Column;

/**
 * 商品分类
 *
 * @author yangylsky
 * @since 2019-08-21 09:26
 *
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("prod_category")
public class Category extends CategoryModel {

	private static final long serialVersionUID = 7548067175241558824L;
	/**
	 * 从8位数字开始的序列，8位数字用完后，支持字母
	 */
	@Column(length = 8)
	private String productSeq;

	public String nextProductSeq() {
		if(StringUtils.isBlank(productSeq)) {
			return "100000001";
		} else {
			String errMsg = "产品序列号超出9位数字，请重新实现本函数";
			if(StringUtils.isNumeric(productSeq)) {
				int seq = Integer.parseInt(productSeq);
				if(seq >= 999999999) {
					throw new BusinessException(errMsg);
				} else {
					return String.valueOf(seq + 1);
				}
			} else {
				throw new BusinessException(errMsg);
			}
		}
	}

	public boolean isTop(){
		return TOP.equals(getParentNo());
	}
}
